<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true"  v-show="showSearch" label-width="80px">
          <el-form-item label="信息标题" prop="newsTitle">
            <el-input v-model="queryParams.newsTitle" placeholder="请输入信息标题" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="所属分类" prop="categoryName">
            <el-input v-model="queryParams.categoryName" placeholder="请输入所属分类" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="信息简介" prop="introduction">
            <el-input v-model="queryParams.introduction" placeholder="请输入信息简介" size="small" clearable style="width: 150px"/>
          </el-form-item>

          <el-form-item label="信息内容" prop="newsContent">
            <el-input v-model="queryParams.newsContent" placeholder="请输入信息内容" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="更新时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" v-hasPermi="['deleteInfoNews']"  icon="el-icon-delete" size="mini" :disabled="single" @click="handleDelete">删除</el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button type="success" v-hasPermi="['recoveryRecycleInfoNews']"  icon="el-icon-share" size="mini" :disabled="single" @click="handleRecovery">恢复</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="queryRecycleInfoNewsPageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table ref="multipleTable" :stripe="true" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange"
                    @sort-change='tableSortChange' :default-sort="{prop: 'sort', order: 'descending'}">
          <el-table-column type="selection" align="center"/>
          <el-table-column label="信息标题" align="left" prop="newsTitle"/>
          <el-table-column label="信息简介" align="left" prop="introduction" width="350"  :show-overflow-tooltip="true"/>
          <el-table-column label="所属分类" align="left" prop="categoryName" width="150"/>
          <el-table-column label="创建时间" align="center" prop="createTime" sortable='custom' width="200" />
          <el-table-column label="更新时间" align="center" prop="deleteTime" sortable='custom' width="200" />
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" v-hasPermi="['deleteInfoNews']" icon="el-icon-delete"  @click="handleDelete(scope.row)">删除</el-button>
              <el-button size="mini" type="text" v-hasPermi="['recoveryRecycleInfoNews']" icon="el-icon-share"  @click="handleRecovery(scope.row)">恢复</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="queryRecycleInfoNewsPageResList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {queryRecycleInfoNewsPageResList,recoveryRecycleInfoNewsByNewsId,deleteInfoNewsByNewsId } from '@/api/info/infoNews'
  //默认查询参数
  let defaultQueryParams = {
    newsContent: undefined,
    introduction: undefined,
    newsTitle: undefined,
    categoryId: undefined,
    categoryName: undefined,
    page: 1,
    rows: 10
  }
  export default {
    name: 'recycleInfoNewsAdmin',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作信息发布
        dataList: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: [],
      }
    },
    created() {
      //查询信息发布列表
      this.queryRecycleInfoNewsPageResList()
    },
    methods: {
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.newsId)
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.queryRecycleInfoNewsPageResList()
      },

      /** 查询信息发布列表 */
      queryRecycleInfoNewsPageResList() {
        this.loading = true
        queryRecycleInfoNewsPageResList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },
      /** 是否删除 */
      handleDelete(row) {
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        this.$confirm('是否确认把所选信息彻底删除?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const newsId={newsId:row.newsId}
          return deleteInfoNewsByNewsId(newsId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.queryRecycleInfoNewsPageResList();
        }).catch(function() {});
      },

      /** 是否恢复 */
      handleRecovery(row) {
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        this.$confirm('是否确认把所选信息恢复正常?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const newsId={newsId:row.newsId}
          return recoveryRecycleInfoNewsByNewsId(newsId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.queryRecycleInfoNewsPageResList();
        }).catch(function() {});
      },


      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = []
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.queryRecycleInfoNewsPageResList()
      }
    }
  }
</script>
